home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Loadstar 6
/
006.d81
/
dos #14
< prev
next >
Wrap
Text File
|
2022-08-26
|
4KB
|
217 lines
DOS & DON'TS -- Part 14
=======================
Now for some Examples! Let's ima-
gine a program that needs to keep tabs
on 100 strings and 100 numbers. Since
(assuming the strings are small) this
can be kept in memory, the data will
be manipulated in the form of a string
array (say, S$) and a numeric array
(say, N). But, the data must be kept
intact between program 'RUNs'. Since
RAM is notorious for forgetting things
every time the power goes or someone
types 'NEW' or 'CLR' or runs another
program, we use a sequential file on
disk to hold the 'goods'.
Let's suppose that this data is in
a file called 'DATAFILE'. We must
first OPEN the CEC and set up some
variables we will need:
10 OPEN 15,8,15,'I0': F$='DATAFILE'
: R$=CHR$(13): DIM S$(100), N(100)
Now let's check to see if the file is
on the disk. We will use the trick we
learned in a previous issue:
20 PRINT#15,'R0:';F$;'=';F$: INPUT
#15, ER%, ER$, ET%, ES%
30 IF ER%=62 THEN 1000: REM FILE
NOT FOUND
40 IF ER%<>63 THEN 2000: REM ERROR
IF NOT A 'FILE EXISTS' MESSAGE.
If the file does not exist, we will
handle that in line 1000. If a disk
error occurred, we will handle that in
line 2000. If the file does exist,
however, we will OPEN it and read its
contents:
50 OPEN 2,8,2,F$: REM NO OPTIONS
NEEDED FOR SEQUENTIAL READ.
60 FOR I=1 TO 100: INPUT#2, S$(I);
N(I): NEXT: CLOSE 2
We have now 'loaded' the arrays with
the contents of the file 'DATAFILE'!
Later on in the program, we have
updated the data and want to write it
out to a new version of 'DATAFILE'.
Here is what we do:
500 PRINT#15, 'S0:'+F$: REM SCRATCH
OLD VERSION FIRST!
510 OPEN 2, 8, 2, F$ + ',S,W': REM
OPEN SEQUENTIAL FILE FOR WRITE.
520 FOR I=1 TO 100: PRINT #2, S$(I)
; R$; N(I): NEXT I: CLOSE 2
Notice the use of the separator
variable R$ (which was assigned a car-
riage return character in line 10) in
line 520.
The routine at line 1000 which han-
dles the case of the 'DATAFILE' not
being on the disk might be like this:
1000 PRINT 'DATAFILE NOT FOUND!':
INPUT 'MAKE ONE FROM SCRATCH? ';A$
1010 IF LEFT$(A$,1)='Y' THEN 1100
1020 PRINT 'PUT IN CORRECT DISK':
PRINT 'THEN PRESS A KEY:'
1030 POKE 198, 0: WAIT 198, 1:
POKE 198, 0: GOTO 20: REM GET KEY.
1100 OPEN 2, 8, 2, F$+',S,W': REM
OPEN FOR SEQUENTIAL WRITE.
1110 FOR I=1 TO 100: PRINT #2,
CHR$(160); R$; 0: NEXT I: CLOSE 2
1120 GOTO 20
Lines 1000-1030 ask the user if the
file needs to be created from scratch.
If not, line 1030 waits for a keypress
then goes back to line 20, which looks
for the file again.
Lines 1100-1120 create a new 'DATA-
FILE'. In line 1110, a CHR$(160), or
'shifted-space' is used for the empty
strings. This is because BASIC skips
over any blank lines in a file! In
all, 100 empty strings and zeros are
written to the file.
The routine at line 2000 handles
disk errors. It checks the value of
ER% (obtained from the CEC in line 30)
and acts upon several possibilities:
2000 IF ER% <> 26 THEN 2100
2010 PRINT 'REMOVE THE DISK, ';
'TAKE OFF THE WRITE-PROTECT TAB,'
2020 PRINT 'THEN REPLACE THE ';
'DISK AND PRESS A KEY:'
2030 POKE 198, 0: WAIT 198, 1:
POKE 198, 0: GOTO 20
2100 IF ER% <> 74 THEN 2200
2110 PRINT 'CLOSE THE DISK';
'DOOR, THEN PRESS A KEY:'
2120 GOTO 2030
2200 PRINT 'DISK ERROR #'; ER%;
'TYPE: '; ER$
2210 PRINT 'ON BLOCK '; ES%;
' OF TRACK ';ET%: END
Lines 2000-2030 check for a Write
Protect tab (error #26). Lines 2100-
2120 handle a 'Drive Not Ready' error
(#74). This is often caused by the
disk drive door being open, or no disk
being in the drive. Lines 2200 & 2210
display other errors, which usually
mean a bug in the program or a bad
disk in the drive.
This concludes this part of our
study of Sequential Files. Hope you
have maybe learned something. Have
fun!!
============End of Article============